﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using System.Data;
using DevExpress.XtraTab;
using DevExpress.XtraEditors.Registrator;
using System.ComponentModel;

namespace ProgNetDxComponents.Controls.Editors
{
    public class MultiLookUpEdit : PopupContainerEdit
    {
        protected override void CreateRepositoryItem()
        {
            this.fProperties = new RepositoryItemMultiLookUpEdit(this);
        }

        [Description("Specifies settings specific to the current editor."),
        Category("Properties"),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemMultiLookUpEdit Properties
        {
            get { return fProperties as RepositoryItemMultiLookUpEdit; }
        }

        protected override void DoShowPopup()
        {
            if (this.Properties.ReadOnly)
            {
                return;
            }
            base.DoShowPopup();
        }
        protected override void OnPopupShown()
        {
            base.OnPopupShown();
            if (this.Properties.ctrl != null)
                this.Properties.ctrl.SelectItem(this.EditValue);
        }
        protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Delete ||
                e.KeyCode == System.Windows.Forms.Keys.Back)
            {
                if (!Properties.ReadOnly)
                    EditValue = "";
            }
            base.OnKeyDown(e);
        }
        #region ISupportInitialize Members

        public void BeginInit()
        {
            this.Properties.BeginInit();
        }

        public void EndInit()
        {
            this.Properties.EndInit();
        }

        #endregion
        public bool ValidateValue()
        {
            return Properties.ValidateValue(this.EditValue);
        }
    }

    [UserRepositoryItem("Register")]
    public class RepositoryItemMultiLookUpEdit : RepositoryItemPopupContainerEdit
    {
        internal MultiLookUpEditPopupContainerControl ctrl = null;

        static RepositoryItemMultiLookUpEdit()
        {
            Register();
        }
        public static void Register()
        {
            EditorRegistrationInfo.Default.Editors.Add(
                new EditorClassInfo("MultiLookUpEdit", typeof(MultiLookUpEdit),
                    typeof(RepositoryItemMultiLookUpEdit),
                    typeof(DevExpress.XtraEditors.ViewInfo.LookUpEditViewInfo),
                    new DevExpress.XtraEditors.Drawing.ButtonEditPainter(),
                    true,
                    null));
        }
        public string Separator
        {
            get { return ctrl.Separator; }
            set { ctrl.Separator = value; }
        }

        private void InitializeControl()
        {
            this.ShowPopupCloseButton = false;
            ctrl = new MultiLookUpEditPopupContainerControl();
            PopupControl = ctrl;
            this.QueryDisplayText += new DevExpress.XtraEditors.Controls.QueryDisplayTextEventHandler(RepositoryItemMultiLookUpEdit_QueryDisplayText);
        }
        public override void Assign(RepositoryItem item)
        {
            base.Assign(item);
            if (item is RepositoryItemMultiLookUpEdit)
            {
                this.ctrl = (item as RepositoryItemMultiLookUpEdit).ctrl;
                this.PopupControl = ctrl;
            }
        }

        void RepositoryItemMultiLookUpEdit_QueryDisplayText(object sender, DevExpress.XtraEditors.Controls.QueryDisplayTextEventArgs e)
        {
            e.DisplayText = ctrl.GetDisplayValue(e.EditValue);
        }

        public void AddGridTab(DataTable dt, string ValueMember, string DisplayMember, string filter, string tabCode, string tabName)
        {
            ctrl.AddGridTab(dt, ValueMember, DisplayMember, filter, tabCode, tabName);
        }

        public void AddTreeTab(DataTable dt, string ValueMember, string DisplayMember, string ParentFieldName, string filter, string tabCode, string tabName)
        {
            ctrl.AddTreeTab(dt, ValueMember, DisplayMember, ParentFieldName, filter, tabCode, tabName);
        }
        public MultiEditBaseTab GetTab(string tabCode)
        {
            return ctrl.GetTab(tabCode);
        }
        public void RemoveTab(string tabCode)
        {
            ctrl.RemoveTab(tabCode);
        }
        public void ClearTabs()
        {
            ctrl.ClearTabs();
        }

        public RepositoryItemMultiLookUpEdit()
        {
            InitializeControl();
        }
        public RepositoryItemMultiLookUpEdit(BaseEdit edit)
        {
            InitializeControl();
            SetOwnerEdit(edit);
        }
        public override BaseEdit CreateEditor()
        {
            return new MultiLookUpEdit();
        }
        public string GetTabCodeFromValue(object editValue)
        {
            return ctrl.GetTabCodeFromValue(editValue);
        }
        public string GetRealValueFromValue(object editValue)
        {
            return ctrl.GetRealValueFromValue(editValue);
        }

        public bool ValidateValue(object editValue)
        {
            string tabCode = GetTabCodeFromValue(editValue);
            string realValue = GetRealValueFromValue(editValue);
            MultiEditBaseTab tab = GetTab(tabCode);
            if (tab != null)
                return tab.ValidateValue(realValue);
            return false;
        }
    }
}
